package com.yuhs.zixing.pay.web.controller.merchant;

import com.alibaba.fastjson.JSON;
import com.yuhs.zixing.pay.dal.merchant.dto.PayMerchantInfoDTO;
import com.yuhs.zixing.pay.service.merchant.MerchantService;
import com.yuhs.zixing.pay.web.controller.merchant.convert.MerchantConvertHelper;
import com.yuhs.zixing.pay.web.controller.merchant.vo.req.MerchantPageQueryVO;
import com.yuhs.zixing.pay.web.controller.merchant.vo.res.MerchantInfoVO;
import com.yuhs.zixing.pay.web.result.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MerchantController
 * @Description 商户控制器
 * @Author yuhs
 * @CreateDate 2020/12/4
 **/
@Slf4j
@RestController
@RequestMapping("/merchant")
public class MerchantController {
    @Resource
    MerchantService merchantService;


    /**
     * 通过商户号获取商户信息
     *
     * @param merchantNo
     * @return
     */
    @RequestMapping("/getMerchantInfoByMerchantNo")
    public DataResult<MerchantInfoVO> getMerchantInfoByMerchantNo(String merchantNo) {
        DataResult<MerchantInfoVO> result = new DataResult<>();
        PayMerchantInfoDTO payMerchantInfoDTO = merchantService.getMerchantInfoByMerchantNo(merchantNo);
        MerchantInfoVO merchantInfoVO = new MerchantInfoVO();
        BeanUtils.copyProperties(payMerchantInfoDTO, merchantInfoVO);
        result.setData(merchantInfoVO);
        return result;
    }

    /**
     * 添加商户信息
     *
     * @param merchantInfoVO
     * @return
     */
    @RequestMapping("/addMerchantInfo")
    public DataResult<MerchantInfoVO> addMerchantInfo(MerchantInfoVO merchantInfoVO) {
        log.info("添加商户信息入参：{}", JSON.toJSONString(merchantInfoVO));
        merchantService.addMerchant(MerchantConvertHelper.addMerchantVO2DTO(merchantInfoVO));
        DataResult<MerchantInfoVO> result = new DataResult<>();
        return result;
    }

    @RequestMapping("queryMerchant")
    public DataResult<List<MerchantInfoVO>> queryMerchant(MerchantPageQueryVO merchantPageQueryVO) {

        log.info("商户分页查询参数 {}", JSON.toJSONString(merchantPageQueryVO));
        List<MerchantInfoVO> merchantInfoVOList = new ArrayList<>(20);
        MerchantInfoVO merchantInfoVO = new MerchantInfoVO();

        PayMerchantInfoDTO payMerchantInfoDTO = merchantService.getMerchantInfoByMerchantNo("1219851901");
        merchantInfoVO = new MerchantInfoVO();
        BeanUtils.copyProperties(payMerchantInfoDTO, merchantInfoVO);
        merchantInfoVOList.add(merchantInfoVO);
        for (int i = 0; i < 19; i++) {
            merchantInfoVO = new MerchantInfoVO();
            merchantInfoVO.setMerchantName("yuhs测试商户" + i);
            merchantInfoVOList.add(merchantInfoVO);
        }

        DataResult<List<MerchantInfoVO>> result = new DataResult<>();
        result.setData(merchantInfoVOList);
        result.setCount(20);
        return result;

    }

}
